package com.demos.cqp.dnt.mvc.controller;

import java.io.IOException;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.demos.cqp.dnt.dao.DAO;
import com.demos.cqp.dnt.dao.DAOException;
import com.demos.cqp.dnt.dao.DvdthequeDAO;
import com.demos.cqp.dnt.domain.DVD;
import com.demos.cqp.dnt.domain.Utilisateur;

/**
 * Servlet implementation class DVDSearchController
 */
@WebServlet("/DVDSearchController")
public class DVDSearchController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@Inject @DAO
	private DvdthequeDAO dao;


    /**
     * @see HttpServlet#HttpServlet()
     */
    public DVDSearchController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String chaineRecherche = request.getParameter("chaine");

		try {
			HttpSession session = request.getSession();
			Utilisateur user = (Utilisateur) session.getAttribute("user");
			session.setAttribute("user", user);
			List<DVD> listeDVD;
			listeDVD = dao.rechercher(chaineRecherche, user);
			request.setAttribute("listeDVD", listeDVD);
			request.getRequestDispatcher("/listeDVD.jsp").forward(request, response);
		} catch (DAOException e) {
			request.setAttribute("error", e.getClass() + " : " + e.getMessage());
			request.getRequestDispatcher("/error.jsp").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
